package com.oneonefive.trainticketsystem.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oneonefive.trainticketsystem.mapper.SaleMapper;
import com.oneonefive.trainticketsystem.pojo.Sale;
import com.oneonefive.trainticketsystem.service.SaleService;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Service
public class SaleServiceImpl extends ServiceImpl<SaleMapper, Sale>
        implements SaleService {
    final
    SaleMapper saleMapper;

    public SaleServiceImpl(SaleMapper saleMapper) {
        this.saleMapper = saleMapper;
    }

    @Override
    public boolean insertSale(Integer saleId, Integer ticketId, String saleTime, Integer userId) {
        Timestamp t1;
        if (saleTime == null || saleTime.length() == 0) {
            Date date = new Date();
            t1 = new Timestamp(date.getTime());
        } else t1 = Timestamp.valueOf(saleTime.substring(0, 10) + " " + saleTime.substring(11, 19));
        return saleMapper.insertSale(saleId, ticketId, t1, userId);
    }

    @Override
    public List<Sale> selectAllSales(Integer saleId, Integer ticketId, Integer userId, Integer page) {
        return saleMapper.selectAllSale(saleId, ticketId, userId, (page - 1) * 10);
    }

    @Override
    public int getSaleRowCount(Integer saleId, Integer ticketId, String saleTime, Integer userId) {
        Timestamp t1;
        if (saleTime == null || saleTime.length() == 0) {
            Date date = new Date();
            t1 = new Timestamp(date.getTime());
        } else t1 = Timestamp.valueOf(saleTime.substring(0, 10) + " " + saleTime.substring(11, 19));
        return saleMapper.getSaleRowCount(saleId, ticketId, t1, userId);
    }
}




